package com.itzb.asone.system.service.impl;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang3.reflect.FieldUtils;
import org.apache.commons.lang3.reflect.MethodUtils;

import com.itzb.asone.annotation.Dict;
import com.itzb.asone.annotation.FileAt;
import com.itzb.asone.mybatis.Criteria;
import com.itzb.asone.system.model.SysDict;
import com.itzb.asone.system.repository.ISysDictRepository;
import com.itzb.asone.system.service.IMessageService;
import com.itzb.asone.system.service.ISysDictService;
import com.itzb.asone.system.service.ISysUploadFileService;

public class SysDictServiceImpl implements ISysDictService {

	private ISysDictRepository sysDictRepository;
	private ISysUploadFileService sysUploadFileService;
	private IMessageService messageService;

	public List<SysDict> queryPage(Criteria<SysDict> param) {
		return sysDictRepository.queryPage(param);
	}

	public Integer queryPageCount(Criteria<SysDict> param) {
		return sysDictRepository.queryPageCount(param);
	}

	public SysDict queryEntityById(String id) {
		return sysDictRepository.queryById(id);
	}

	public Integer insert(SysDict record) {
		return sysDictRepository.insert(record);
	}

	public Integer updateByCriteria(Criteria<SysDict> param) {
		return sysDictRepository.updateByCriteria(param);
	}

	public Integer deleteById(String id) {
		return sysDictRepository.deleteById(id);
	}

	public Integer deleteByCriteria(Criteria<SysDict> param) {
		return sysDictRepository.deleteByCriteria(param);
	}

	public ISysDictRepository getSysDictRepository() {
		return sysDictRepository;
	}

	public void setSysDictRepository(ISysDictRepository sysDictRepository) {
		this.sysDictRepository = sysDictRepository;
	}

	public IMessageService getMessageService() {
		return messageService;
	}

	public void setMessageService(IMessageService messageService) {
		this.messageService = messageService;
	}
	
	public ISysUploadFileService getSysUploadFileService() {
		return sysUploadFileService;
	}

	public void setSysUploadFileService(ISysUploadFileService sysUploadFileService) {
		this.sysUploadFileService = sysUploadFileService;
	}

	public <T> List<Map<String, Object>> translateToMapList(List<T> data)
	{

		List<Map<String, Object>> ret = new ArrayList<Map<String, Object>>();

		if (data == null || data.isEmpty())
		{
			return ret;
		}

		Class<?> rowClazz = data.get(0).getClass();
		Field[] fields = FieldUtils.getAllFields(rowClazz);
		if (fields == null || fields.length < 1)
		{
			return ret;
		}

		for (Object rowData : data)
		{
			Map<String, Object> row = translateToMap(rowData);
			if (row != null)
			{
				ret.add(row);
			}
		}

		return ret;
	}
	
	public Map<String, Object> translateToMap(Object obj)
	{

		Field[] fields = FieldUtils.getAllFields(obj.getClass());
		if (fields == null || fields.length < 1)
		{
			return null;
		}

		Map<String, Object> row = new HashMap<String, Object>();

		for (int i = 0; i < fields.length; i++)
		{

			Field aField = fields[i];
			Dict config = aField.getAnnotation(Dict.class);
			FileAt fileAt = aField.getAnnotation(FileAt.class);

			String fieldName = aField.getName();
			String methodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1, fieldName.length());
			try
			{
				Object value = MethodUtils.invokeMethod(obj, methodName);
				row.put(fieldName, value);

				if (config != null && config.dictCode()!=null)
				{

					if (value == null)
					{
						row.put(fieldName + "Zh", "");
						continue;
					}

					row.put(fieldName + "Zh", cachedValue(config.dictCode(), value.toString()));
				}
				
				if (fileAt != null)
				{

					if (value == null)
					{
						row.put(fieldName + "Url", "");
						continue;
					}

					row.put(fieldName + "Url", sysUploadFileService.getFileUrl(value.toString()));
				}

			}
			catch (Exception e)
			{
			}
			
		}
		return row;
	}
	
	
	public String cachedValue(String key, String value){
		String cachedValue = "";
		Criteria<SysDict> param = new Criteria<SysDict>();
		param.initParams().addParam("dictCode", key);
		param.addParam("dictValue", value);
		param.setPageSize(1);
		param.setStartIndex(0);

		List<SysDict> tmp = sysDictRepository.queryPage(param);
		if (tmp != null && !tmp.isEmpty())
		{
			cachedValue = tmp.get(0).getDictRemark() != null ? tmp.get(0).getDictRemark() : "";
		}
		else
		{
			cachedValue = "";
		}
		
		return cachedValue;
	}
	
	public List<Map<String, Object>> translateToMapList(List<Map<String, Object>> data, Map<String, String> transColumns){

		if (data == null || data.isEmpty())
		{
			return data;
		}

		if (transColumns == null || transColumns.isEmpty())
		{
			return data;
		}

		for (Map<String, Object> row : data)
		{
			translateToMap(row, transColumns);
		}

		return data;
	}
	
	public Map<String, Object> translateToMap(Map<String, Object> obj, Map<String, String> transColumns) {

		if (obj == null || obj.isEmpty())
		{
			return obj;
		}

		if (transColumns == null || transColumns.isEmpty())
		{
			return obj;
		}

		for (Entry<String, String> column : transColumns.entrySet())
		{
			if (obj.containsKey(column.getKey()) && null != obj.get(column.getKey()))
			{
				if(transColumns.get(column.getKey()).lastIndexOf("_Url") == transColumns.get(column.getKey()).length()-4){
					if(obj.get(column.getKey())!=null && !obj.get(column.getKey()).equals("")){
						obj.put(column.getKey() + "Url", sysUploadFileService.getFileUrl(obj.get(column.getKey()).toString()));
					}
				} else {
					obj.put(column.getKey() + "Zh", cachedValue(column.getValue(), obj.get(column.getKey()).toString()));
				}
			}
		}
		return obj;
	}
}
